﻿using System;
using System.Globalization;

namespace Microsoft.International.Formatters
{

    /// <summary>
    /// English (en-US) formatter
    /// </summary>
    [FormatCultureInfoAttribute("en-US")]
    internal sealed class ENStdFormatter : EuropeFormatter
    {

        /// <summary>
        /// Returns the Decimal value to String.
        /// </summary>
        /// <param name="num">Value to translate.</param>
        /// <returns>Returns the specified Decimal value to String.</returns>
        /// <remarks>Abstract method, must be implemented.</remarks>
        internal override String ConvertToLocalizedText(Decimal num)
        {
            String strMoins = String.Empty;
            Int32 value = (Int32)num; // TODO: Supprimer cette ligne
            if (value < 0)
            {
                strMoins = "negative ";
                if (value == Int32.MinValue)
                {
                    strMoins += "two billion ";
                    value += 2000000000;
                }
                value = Math.Abs(value);
            }

            return strMoins + BillionToString(value);
        }

        /// <summary>
        /// Convert units (Int32 format) to String format.
        /// </summary>
        /// <param name="value">Value to convert</param>
        /// <returns>Returns a text which represents the specified value</returns>
        private String UnitToString(Int32 value)
        {
            String strMoins = String.Empty;
            if (value < 0)
            {
                strMoins = "minus ";
                value = Math.Abs(value);
            }

            if (value > 9)
                return strMoins + TenToString(value);

            String result = String.Empty;

            switch (value.ToString(new CultureInfo("en-US")))
            {
                case "0": result = "zero"; break;
                case "1": result = "one"; break;
                case "2": result = "two"; break;
                case "3": result = "three"; break;
                case "4": result = "four"; break;
                case "5": result = "five"; break;
                case "6": result = "six"; break;
                case "7": result = "seven"; break;
                case "8": result = "eight"; break;
                case "9": result = "nine"; break;
            }

            return strMoins + result;
        }

        /// <summary>
        /// Convert tens (Int32 format) to String format.
        /// </summary>
        /// <param name="value">Value to convert</param>
        /// <returns>Returns a text which represents the specified value</returns>
        private String TenToString(Int32 value)
        {
            if (value < 10)
                return UnitToString(value);
            else if (value > 99)
                return HundredToString(value);

            String result = String.Empty;

            switch (value)
            {
                case 10: result = "ten"; break;
                case 11: result = "eleven"; break;
                case 12: result = "twelve"; break;
                case 13: result = "thirteen"; break;
                case 14: result = "fourteen"; break;
                case 15: result = "fifteen"; break;
                case 16: result = "sixteen"; break;
                case 17: result = "seventeen"; break;
                case 18: result = "eighteen"; break;
                case 19: result = "nineteen"; break;

                case 20: result = "twenty"; break;
                case 30: result = "thirty"; break;
                case 40: result = "fourty"; break;
                case 50: result = "fifty"; break;
                case 60: result = "sixty"; break;
                case 70: result = "seventy"; break;
                case 80: result = "eighty"; break;
                case 90: result = "ninety"; break;
                default:
                    result = String.Format(new CultureInfo("en-US"), "{0}-{1}", TenToString(value.GetTen()), UnitToString(value.GetUnit()));
                    break;
            }

            return result;
        }

        /// <summary>
        /// Convert hundreds (Int32 format) to String format.
        /// </summary>
        /// <param name="value">Value to convert</param>
        /// <returns>Returns a text which represents the specified value</returns>
        private String HundredToString(Int32 value)
        {
            if (value < 100)
                return TenToString(value);
            else if (value > 999)
                return ThousandToString(value);

            Int32 reste = 0;
            Int32 centaine = System.Math.DivRem(value, 100, out reste);

            String strCentaine = String.Format(new CultureInfo("en-US"), "{0} hundred", TenToString(centaine));

            if (reste != 0)
                strCentaine = String.Format(new CultureInfo("en-US"), "{0} {1}", strCentaine, TenToString(reste));

            return strCentaine;

        }

        /// <summary>
        /// Convert thousands (Int32 format) to String format.
        /// </summary>
        /// <param name="value">Value to convert</param>
        /// <returns>Returns a text which represents the specified value</returns>
        private String ThousandToString(Int32 value)
        {
            if (value < 1000)
                return HundredToString(value);
            else if (value > 999999)
                return MillionToString(value);

            Int32 reste = 0;
            Int32 millier = System.Math.DivRem(value, 1000, out reste);

            String strMillier = String.Format(new CultureInfo("en-US"), "{0} thousand", HundredToString(millier));

            if (reste != 0)
                strMillier = String.Format(new CultureInfo("en-US"), "{0} {1}", strMillier, HundredToString(reste));

            return strMillier;
        }

        /// <summary>
        /// Convert millions (Int32 format) to String format.
        /// </summary>
        /// <param name="value">Value to convert</param>
        /// <returns>Returns a text which represents the specified value</returns>
        private String MillionToString(Int32 value)
        {
            if (value < 1000000)
                return ThousandToString(value);
            else if (value > 999999999)
                return BillionToString(value);

            Int32 reste = 0;
            Int32 millions = System.Math.DivRem(value, 1000000, out reste);

            String strMillions = String.Format(new CultureInfo("en-US"), "{0} million", ThousandToString(millions));

            if (reste != 0)
                strMillions = String.Format(new CultureInfo("en-US"), "{0} {1}", strMillions, ThousandToString(reste));

            return strMillions;
        }

        /// <summary>
        /// Convert billions (Int32 format) to String format.
        /// </summary>
        /// <param name="value">Value to convert</param>
        /// <returns>Returns a text which represents the specified value</returns>
        private String BillionToString(Int32 value)
        {
            if (value < 1000000000)
                return MillionToString(value);

            Int32 reste = 0;
            Int32 milliard = System.Math.DivRem(value, 1000000000, out reste);

            String strMilliard = String.Format(new CultureInfo("en-US"), "{0} billion", MillionToString(milliard));

            if (reste != 0)
                strMilliard = String.Format(new CultureInfo("en-US"), "{0} {1}", strMilliard, MillionToString(reste));

            return strMilliard;
        }

    }

}